import os
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

import account
from account import Account 

class MainPage(webapp.RequestHandler):
   def get(self):

      if users.get_current_user():
         url = users.create_logout_url("/")
         
         accounts = db.GqlQuery("SELECT * FROM Account WHERE owner = :owner", owner = users.get_current_user());
         
         template_parameters = {
            'logout_url': url,
            'accounts': accounts,
         }
         path = os.path.join(os.path.dirname(__file__), 'templates/index.html')
         self.response.out.write(template.render(path, template_parameters))
      else:
         url = users.create_login_url(self.request.uri)

         template_parameters = {
            'login_url': url,
         }
         path = os.path.join(os.path.dirname(__file__), 'templates/welcome.html')
         self.response.out.write(template.render(path, template_parameters))

class AccountRegister(webapp.RequestHandler):
   def get(self):
      template_parameters = {
         'readonly': '',
         'username': '',
         'password': '',
         'action': '/account/register',
      }
      path = os.path.join(os.path.dirname(__file__), 'templates/account_form.html')
      self.response.out.write(template.render(path, template_parameters))

   def post(self):
      if users.get_current_user():
# TODO: That's probably not the best way to handle the duplicates.
         account = db.GqlQuery("SELECT __key__ FROM Account WHERE owner = :owner AND username = :username",
            owner = users.get_current_user(), username = self.request.get("username")).get();
# TODO: We probably need some message if the account is already registered.
         if not account:
            account = Account(owner = users.get_current_user(), username = self.request.get("username"), password = self.request.get("password"))
            account.put()
         self.redirect("/")
      else:
         self.error(403)

class AccountDelete(webapp.RequestHandler):
   def get(self):
      if users.get_current_user():
# TODO: That's probably not the best way to handle permissioning.
         account = db.GqlQuery("SELECT __key__ FROM Account WHERE owner = :owner AND __key__ = :key",
            owner = users.get_current_user(), key = db.Key(self.request.get("key"))).get();
#         if account:
         db.delete(account)
# TODO: We probably need some message if the account is not registered.
      self.redirect("/")

class AccountMail(webapp.RequestHandler):
   def get(self):
      if users.get_current_user():
         self.redirect("/")
      else:
         self.redirect("/")

application = webapp.WSGIApplication([
                                       ('/', MainPage),
                                       ('/account/delete', AccountDelete),
                                       ('/account/register', AccountRegister),
                                       ('/account/mail', AccountMail),
                                     ], debug=True)

def main():
   run_wsgi_app(application)

if __name__ == "__main__":
   main()
